﻿#pragma execution_character_set("utf-8")
#include "config_dialog.h"
#include <QFile>
#include <QApplication>
#include <QJsonDocument>
#include <QJsonArray>
#include <QtDebug>
#include <QStandardPaths>
#include <QDir>
ConfigDialog::ConfigDialog(QString strConfigDir,QString strConfigFileName, QWidget *parent)
    : QDialog(parent)
    , m_strConfigDir(strConfigDir)
    , m_strConfigFileName(strConfigFileName)
    , m_resultItem(new ConfigItemWidget())
    , m_layoutMain(new QVBoxLayout())
    , m_btnSave(new QPushButton("保存配置"))
{
    setWindowTitle("配置");
    initUI();
    initConnect();

}

ConfigDialog::ConfigDialog(QWidget *parent)
: QDialog(parent)
{

}

void ConfigDialog::slotClickSaveConfig()
{
    // 保存配置文件
    QString configDir = getConfigDir();
    QDir dir(configDir);
    if(!dir.exists()){
        dir.mkpath(configDir);
    }
    QString configFileName = configDir + "/" + m_strConfigFileName;
}

void ConfigDialog::initUI()
{
    QString fileName = ":/Config/"+m_strConfigDir+"/"+m_strConfigFileName;
    QFile file(fileName);
    if(file.open(QFile::ReadOnly)){
       QString strJson = file.readAll();
       QJsonDocument jdoc = QJsonDocument::fromJson(strJson.toUtf8());
       QJsonObject jobject = jdoc.object();
       QJsonValue jvalue_result = jobject.value("result");
       QJsonObject jobject_result = jvalue_result.toObject();
       qInfo() << jobject_result.value("name").toInt();
       qInfo() << jobject_result.value("max").toInt();
       qInfo() << jobject_result.value("min").toInt();
       m_resultItem->setData(jobject_result.value("name").toString()
                              ,"最小值"
                              ,jobject_result.value("min").toInt()
                              ,"最大值"
                              ,jobject_result.value("max").toInt());
       m_layoutMain->addWidget(m_resultItem);

       QJsonArray jnum_lists = jobject.value("num").toArray();
       for (auto item : jnum_lists) {
            QJsonObject itemObj = item.toObject();
            ConfigItemWidget *tempItem = new ConfigItemWidget();
            qWarning()<<"name:"<< itemObj.value("name").toString();
            qWarning()<<"max:"<< itemObj.value("max").toInt();
            qWarning()<<"min:"<< itemObj.value("min").toInt();
            tempItem->setData(itemObj.value("name").toString()
                                   ,"最小值"
                                   ,itemObj.value("min").toInt()
                                   ,"最大值"
                                   ,itemObj.value("max").toInt());
            m_vectNums.push_back(tempItem);
            m_layoutMain->addWidget(tempItem);
       }
       file.close();
    }
    m_layoutMain->addWidget(m_btnSave);
    setLayout(m_layoutMain);
    qAppName();
}

void ConfigDialog::initConnect()
{
    connect(m_btnSave,&QPushButton::clicked,this,&ConfigDialog::slotClickSaveConfig);
}

QString ConfigDialog::getConfigDir()
{
    QString HomeLocation =  QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
    qWarning() << "HomeLocation=" << HomeLocation;
    QString configDir = HomeLocation + "/.config/school/"+m_strConfigDir+"/";
    return configDir;
}

QString ConfigDialog::getConfigFilePath()
{
    QString filePath = getConfigDir() + "/" + m_strConfigFileName;
    return filePath;
}
